package com.binguo.model.mybatis;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Criteria {
	protected List<String> criteria;

    protected Criteria() {
        criteria = new ArrayList<>();
    }

    public boolean isValid() {
        return criteria.size() > 0;
    }

    public List<String> getAllCriteria() {
        return criteria;
    }

    public List<String> getCriteria() {
        return criteria;
    }

    public Criteria andIsNull(String property) {
    	criteria.add(dbProperty(property) + " is null");
        return this;
    }

    public Criteria andIsNotNull(String property) {
    	criteria.add(dbProperty(property) + " is not null");
        return this;
    }
    
    public Criteria andEqualTo(String property) {
    	criteria.add(dbProperty(property) + "=#{domain." +  property + "}");
        return this;
    }
    
    public Criteria andIdEqualTo() {
    	return this.andEqualTo("id");
    }
    
    public Criteria andLike(String property,String value) {
    	criteria.add(dbProperty(property) + " like '%" + value + "%'");
    	return this;
    }

    public Criteria andNotEqualTo(String property) {
    	criteria.add(dbProperty(property) + "<>#{domain." +  property + "}");
        return this;
    }

    public Criteria andGreaterThan(String property) {
    	criteria.add(dbProperty(property) + ">#{domain." +  property + "}");
        return this;
    }

    public Criteria andGreaterThanOrEqualTo(String property) {
    	criteria.add(dbProperty(property) + ">=#{domain." +  property + "}");
        return this;
    }

    public Criteria andLessThan(String property) {
    	criteria.add(dbProperty(property) + "<#{domain." +  property + "}");
        return this;
    }

    public Criteria andLessThanOrEqualTo(String property) {
    	criteria.add(dbProperty(property) + "<=#{domain." +  property + "}");
        return this;
    }
    
    public Criteria andIn(String property,Integer[] values) {
    	criteria.add(property + " in (" + Arrays.stream(values).map(v->v.toString()).collect(Collectors.joining(",")) + ")");
    	return this;
    }

    /**
     * 2018-07-09 修改参数为stddomain 传入start,end
     */
    public Criteria andBetween(String property,StdDomain addition) {
    	criteria.add(dbProperty(property) + " between '" +  addition.getStart() + "' and '" + addition.getEnd() + "'");
        return this;
    }

    /**
     * 2018-07-09 修改参数为stddomain 传入start,end
     */
    public Criteria andNotBetween(String property,StdDomain addition) {
    	criteria.add(dbProperty(property) + " not between '" +  addition.getStart() + "' and '" + addition.getEnd() + "'");
        return this;
    }
    
    
    private String dbProperty(String property) {
    	return CamelStringUtils.toUnderlineName(property);
    }
}
